package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.bo.ZsyyCommunityUserAddBo;
import com.ruoyi.system.bo.ZsyyCommunityUserQueryBo;
import com.ruoyi.system.bo.ZsyyCommunityUserEditBo;
import com.ruoyi.system.domain.ZsyyCommunityUser;
import com.ruoyi.system.mapper.ZsyyCommunityUserMapper;
import com.ruoyi.system.vo.ZsyyCommunityUserVo;
import com.ruoyi.system.service.IZsyyCommunityUserService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 业主信息管理Service业务层处理
 *
 * @author wangghua
 * @date 2021-07-14
 */
@Service
public class ZsyyCommunityUserServiceImpl extends ServicePlusImpl<ZsyyCommunityUserMapper, ZsyyCommunityUser> implements IZsyyCommunityUserService {
    @Autowired
    private ZsyyCommunityUserMapper iZsyyCommunityUserMapper;
	@Autowired
	private GetCommunityIdImpl getCommunityIdImpl;

    @Override
    public ZsyyCommunityUserVo queryById(Long id){
        return getVoById(id, ZsyyCommunityUserVo.class);
    }

    @Override
    public TableDataInfo<ZsyyCommunityUserVo> queryPageList(ZsyyCommunityUserQueryBo bo) {
		bo.setCommunityId(getCommunityIdImpl.getLoginUser().getCommunityId());
		Page<ZsyyCommunityUserVo> result = iZsyyCommunityUserMapper.selectListByQuery(PageUtils.buildPage(), bo);
		return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<ZsyyCommunityUserVo> queryList(ZsyyCommunityUserQueryBo bo) {
		return iZsyyCommunityUserMapper.queryListByExport(bo);
    }

    private LambdaQueryWrapper<ZsyyCommunityUser> buildQueryWrapper(ZsyyCommunityUserQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZsyyCommunityUser> lqw = Wrappers.lambdaQuery();
		bo.setCommunityId(getCommunityIdImpl.getLoginUser().getCommunityId());
        lqw.like(StrUtil.isNotBlank(bo.getName()), ZsyyCommunityUser::getName, bo.getName());
        lqw.eq(StrUtil.isNotBlank(bo.getPhone()), ZsyyCommunityUser::getPhone, bo.getPhone());
        lqw.eq(bo.getStatus() != null, ZsyyCommunityUser::getStatus, bo.getStatus());
        lqw.eq(bo.getCommunityId() != null, ZsyyCommunityUser::getCommunityId, bo.getCommunityId());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(ZsyyCommunityUserAddBo bo) {
        ZsyyCommunityUser add = BeanUtil.toBean(bo, ZsyyCommunityUser.class);
		add.setStatus(2);//后台添加默认通过
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(ZsyyCommunityUserEditBo bo) {
        ZsyyCommunityUser update = BeanUtil.toBean(bo, ZsyyCommunityUser.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ZsyyCommunityUser entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

    /**
	 *  TODO 未完成更新人脸
	 */
	@Override
	public Boolean updateFace(Long id, boolean isValid) {
		if(isValid){

		}
		return true;
	}

	/**
	 *  TODO 未完成添加卡
	 *
	 */
	@Override
	public Boolean addCardNum(ZsyyCommunityUserEditBo bo) {
		return null;
	}
}
